// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.reports.editor.design.figures;

import org.dengues.reports.editor.ReportColorConstants;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.TreeSearch;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Point;

/**
 * Report Editot root editpart.
 * 
 * 
 * /** Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-1-26 qiang.zhang $
 * 
 */
public class ReportRootFigure extends ReportElementFigure {

    private static final Insets DEFAULT_MARGIN = new Insets(3, 3, 3, 3);

    private static final Point PRIVATE_POINT = new Point();

    private static final Insets DEFAULT_CROP = new Insets(-3, -3, -2, -2);

    private boolean showMargin;

    /**
     * 
     */
    public ReportRootFigure() {

    }

    /*
     * Do nothing
     * 
     * @see org.eclipse.draw2d.Figure#paintBorder(org.eclipse.draw2d.Graphics)
     */
    protected void paintBorder(Graphics graphics) {
        // does nothing , figure paint itself.
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.draw2d.IFigure#getInsets()
     */
    public Insets getInsets() {
        if (showMargin) {
            if (getBorder() != null) {
                return getBorder().getInsets(this);
            }

            return NO_INSETS;
        }
        return DEFAULT_MARGIN;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.draw2d.Figure#paintFigure(org.eclipse.draw2d.Graphics)
     */
    protected void paintFigure(Graphics graphics) {
        super.paintFigure(graphics);
        graphics.setForegroundColor(ReportColorConstants.MarginBorderColor);
        graphics.drawRectangle(getBounds().getCopy().crop(getInsets()).crop(DEFAULT_CROP));
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.draw2d.Figure#paintChildren(org.eclipse.draw2d.Graphics)
     */
    protected void paintChildren(Graphics graphics) {
        IFigure child;

        for (int i = 0; i < this.getChildren().size(); i++) {
            child = (IFigure) this.getChildren().get(i);
            if (child.isVisible()) {
                graphics.setClip(getBounds().getCopy().intersect(child.getBounds()));
                child.paint(graphics);
                graphics.restoreState();
            }
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.draw2d.Figure#findDescendantAtExcluding(int, int, org.eclipse.draw2d.TreeSearch)
     */
    protected IFigure findDescendantAtExcluding(int x, int y, TreeSearch search) {
        PRIVATE_POINT.setLocation(x, y);
        translateFromParent(PRIVATE_POINT);
        if (!getBounds().contains(PRIVATE_POINT))
            return null;

        IFigure fig;
        for (int i = getChildren().size(); i > 0;) {
            i--;
            fig = (IFigure) getChildren().get(i);
            if (fig.isVisible()) {
                fig = fig.findFigureAt(PRIVATE_POINT.x, PRIVATE_POINT.y, search);
                if (fig != null)
                    return fig;
            }
        }
        // No descendants were found
        return null;
    }

    /**
     * @return Returns the showMargin.
     */
    public boolean isShowMargin() {
        return showMargin;
    }

    /**
     * @param showMargin The showMargin to set.
     */
    public void setShowMargin(boolean showMargin) {
        this.showMargin = showMargin;
    }
}
